home *** CD-ROM | disk | FTP | other *** search
- #include "BuildControl.h"
-
-
- #if defined(qUseDumpFile)
- #include "DumpHeader.h"
- #else
- #include <QuickDraw.h>
- #include <Dialogs.h>
- #include <Fonts.h>
- #include <Processes.h>
- #include <TextEdit.h>
- #include <Events.h>
- #include <Menus.h>
- #include <Memory.h>
- #include <Resources.h>
- #include <Errors.h>
- #include <ToolUtils.h>
- #endif
-
- #include "WindowObj.h"
- #include "AESupport.h"
- #include "FloatingWindowSupport.h"
-
- #include "MainWindow.h"
- #include "QuickDraw3DSupport.h"
-
- #ifdef __powerc
- QDGlobals qd;
- #endif
-
-
- //
- // ••• CHANT THE FOLLOWING LINES EVERY TIME YOU THINK ABOUT CHANGING THIS CODE •••
- //
- // • This is a barebones shell.
- // • This is NOT designed to do everything for everyone.
- // • This does not contain the one true way to do anything.
- // • This code should err on the side of simplicity and size versus
- // functionality and bloat.
- //
-
-
- //
- // General event handlers/other
- //
- void InitApplication(void);
- void PreEventLoop(void);
- void MainEventLoop(void);
- void PostEventLoop(void);
- void GeneralAdjustMenus(void);
- void NotifyWindow(WindowPtr win, long message);
- void DoClick(WindowPtr win, EventRecord *event, long message);
- void DoAdjustMenus(void);
- void DoActivate(WindowPtr win, Boolean activate);
- void DoUpdate(WindowPtr thisWindow);
- void DoIdle(void);
- void DoClose(WindowPtr win);
- void DoKeys(long message, short modifiers);
- void DoEditMenu(WindowPtr win, short item);
- pascal void DoDeviceLoopDraw(short, short, GDHandle, long);
- void MenuCommand(long);
-
- //
- // Specific event handlers for the AboutBox window.
- //
- void DoAboutBox(void);
-
- void AboutBoxDraw(WindowObjHndl obj, short depth);
- void AboutBoxClick(WindowObjHndl obj, EventRecord *event, long message);
- void AboutBoxActivate(WindowObjHndl obj, Boolean activate);
- void AboutBoxKeys(WindowObjHndl obj, long message, short mods);
- void AboutBoxNotify(WindowObjHndl obj, long message);
-
-
- Boolean gDone;
- static Boolean gInBackground;
-
-
-
- #pragma segment Main
-
- //-----------------------------------------------------------------------
- // This will get called whenever there are no windows showing, or the
- // frontmost window is someone else's. Perform menu adjustments
- // accordingly.
- //-----------------------------------------------------------------------
- void GeneralAdjustMenus(void)
- {
- MenuHandle fileMenu = GetMenuHandle(kFileMenu);
-
- if (fileMenu != NULL) {
- DisableItem(fileMenu, kCloseItem); DisableItem(fileMenu, kOpenItem);
- EnableItem(fileMenu, kNewItem); EnableItem(fileMenu, kQuitItem);
- }
- }
-
- //-----------------------------------------------------------------------
- // Anything to setup before the event loop? Do it here.
- //-----------------------------------------------------------------------
- void PreEventLoop(void)
- {
- InitAESupport();
- InitQuickDraw3DSupport();
- }
-
- //-----------------------------------------------------------------------
- // Anything to teardown after the event loop? Do it here.
- //-----------------------------------------------------------------------
- void PostEventLoop(void)
- {
- TeardownQuickDraw3DSupport();
- }
-
- //-----------------------------------------------------------------------
-
- void NotifyWindow(WindowPtr win, long message)
- {
- WindowObjHndl obj;
-
- if (IsAppWindow(win)) {
- obj = (WindowObjHndl) GetWRefCon(win);
-
- if ((obj != NULL) && ((*obj)->notify != NULL)) {
- //
- // If the message is not an idle message, send it through,
- // or if it is, only send it if the object has an idle task.
- //
- // We do this to minimize the amount of idle time taken by
- // this app. If we don't implement this type of check, the
- // amount of idle time taken by this app will grow when it
- // really doesn't need to. If a window needs idle time,
- // it should set the hasIdleTask flag in its ObjHndl.
- //
- if ((message != kIdleNotification)
- || ((message == kIdleNotification) && ((*obj)->hasIdleTask == true))) {
- GrafPtr oldPort = SetupPort(win);
- (*(*obj)->notify)(obj, message);
- SetPort(oldPort);
- }
- }
- }
- }
-
- //-----------------------------------------------------------------------
- // Walk our window list, and if any window has an idle handler call it.
- //
- // For most things, you should modify the window's specific idle
- // routine instead of touching this.
- //-----------------------------------------------------------------------
- void DoIdle(void)
- {
- WindowPtr win;
- GrafPtr oldPort;
-
- GetPort(&oldPort);
-
- for (win = FrontWindow(); win != NULL;
- win = (WindowPtr) ((WindowPeek) win)->nextWindow) {
- SetPort(win);
- NotifyWindow(win, kIdleNotification);
- }
-
- SetPort(oldPort);
- }
-
- //-----------------------------------------------------------------------
- // Main dispatch routine to the window's activate routine.
- //
- // For most things, you should modify the window's specific activate
- // routine instead of touching this.
- //-----------------------------------------------------------------------
- void DoActivate(WindowPtr win, Boolean activate)
- {
- WindowObjHndl obj = (WindowObjHndl) GetWRefCon(win);
-
- if ((obj != NULL) && ((*obj)->activate != NULL)) {
- GrafPtr oldPort = SetupPort(win);
- (*(*obj)->activate)(obj, activate);
- SetPort(oldPort);
- }
- }
-
- //-----------------------------------------------------------------------
- // Main dispatch routine to the window's close routine.
- //
- // For most things, you should modify the window's specific close
- // code instead of touching this.
- //-----------------------------------------------------------------------
- void DoClose(WindowPtr win)
- {
- GrafPtr oldPort = SetupPort(win);
-
- #if defined(qFloatingWindowSupport)
- HideThisWindow(win);
- #endif
-
- NotifyWindow(win, kCloseNotification);
- SetPort(oldPort);
- }
-
- //-----------------------------------------------------------------------
- // Main dispatch routine to the window's AdjustMenu routine.
- //
- // For most things, you should modify the window's specific AdjustMenu
- // code instead of touching this.
- //-----------------------------------------------------------------------
- void DoAdjustMenus(void)
- {
- #if defined (qFloatingWindowSupport)
- WindowPtr win = FrontNonFloatingWindow();
- #else
- WindowPtr win = FrontWindow();
- #endif
-
- if (IsAppWindow(win))
- NotifyWindow(win, kAdjustMenusNotification);
- else
- //
- // OK, we're probably in a state where there is no
- // window showing, in which case we need to call the
- // GlobalAdjustMenus routine instead.
- //
- GeneralAdjustMenus();
- }
-
- //-----------------------------------------------------------------------
- // Main dispatch routine to the window's click routine.
- //
- // For most things, you should modify the window's specific click
- // routine instead of touching this.
- //-----------------------------------------------------------------------
- void DoClick(WindowPtr win, EventRecord *event, long message)
- {
- WindowObjHndl obj = (WindowObjHndl) GetWRefCon(win);
-
- if ((obj != NULL) && ((*obj)->click != NULL)) {
- GrafPtr oldPort = SetupPort(win);
- (*(*obj)->click)(obj, event, message);
- SetPort(oldPort);
- }
- }
-
- //-----------------------------------------------------------------------
- // If we received a non-command key event, pass it to the window if
- // it wants to handle it.
- //
- // For most things, you should modify the window's specific keys
- // routine instead of touching this.
- //-----------------------------------------------------------------------
- void DoKeys(long message, short modifiers)
- {
- WindowPtr win = FrontNonFloatingWindow();
-
- if (IsAppWindow(win)) {
- WindowObjHndl obj = (WindowObjHndl) GetWRefCon(win);
-
- if ((obj != NULL) && ((*obj)->keys != NULL)) {
- GrafPtr oldPort = SetupPort(win);
- (*(*obj)->keys)(obj, message, modifiers);
- SetPort(oldPort);
- }
- }
- }
-
- //-----------------------------------------------------------------------
- // Main dispatch routine to the window's click routine.
- //
- // For most things, you should modify the window's specific update
- // routine instead of touching this.
- //-----------------------------------------------------------------------
- void DoUpdate(WindowPtr win)
- {
- static DeviceLoopDrawingUPP drawProc = NULL;
-
- check(win != NULL);
- SetPort(win);
-
- if (drawProc == NULL)
- drawProc = NewDeviceLoopDrawingProc(DoDeviceLoopDraw);
-
- BeginUpdate(win);
- DeviceLoop(win->visRgn, drawProc, (long) win, singleDevices);
- EndUpdate(win);
- }
-
- //-----------------------------------------------------------------------
- // Main dispatch routine to the window's edit code
- //
- // For most things, you should modify the window's specific edit
- // notification handler instead of touching this.
- //
- // Why aren't we just sending the raw menu item to the window?
- // Because we want to separate the act of editing the window using
- // common menu commands from the menu items. This will make life
- // easier down the road if we make this scriptable.
- //-----------------------------------------------------------------------
- void DoEditMenu(WindowPtr win, short item)
- {
- long message;
-
- if (IsAppWindow(win)) {
- switch (item) {
- case kUndoItem: message = kUndoNotification; break;
- case kCutItem: message = kCutNotification; break;
- case kCopyItem: message = kCopyNotification; break;
- case kPasteItem: message = kPasteNotification; break;
- case kClearItem: message = kClearNotification; break;
- case kSelectAllItem:message = kSelectAllNotification; break;
- }
- NotifyWindow(win, message);
- }
- }
-
- //-----------------------------------------------------------------------
- // This is bit nicer interface than DeviceLoop's drawing proc (and it
- // sets thePort for all windows about to be drawn to.)
- //
- // For most things, you should modify the window's specific update
- // routine instead of touching this.
- //-----------------------------------------------------------------------
- pascal void DoDeviceLoopDraw( short pixelDepth,
- short dFlags,
- GDHandle theDevice,
- long theWin)
- {
- #pragma unused (dFlags, theDevice)
- GrafPtr oldPort;
- WindowPtr win = (WindowPtr) theWin;
- WindowObjHndl obj;
-
- oldPort = SetupPort(win);
- obj = (WindowObjHndl) GetWRefCon(win);
-
- if ((obj != NULL) && ((*obj)->draw != NULL))
- (*(*obj)->draw)(obj, pixelDepth);
-
- SetPort(oldPort);
- }
-
- //-----------------------------------------------------------------------
- // Put up the window, allocate the window data and set its fields
- //-----------------------------------------------------------------------
- WindowObjHndl NewObjWindow( short windID,
- OSType type,
- Boolean itFloats,
- Boolean hasIdleTask,
- DrawContentProcPtr draw,
- KeystrokeProcPtr keys,
- ClickProcPtr click,
- ActivateProcPtr activate,
- NotifyProcPtr notify)
- {
- WindowPtr win, listPosition = NULL;
- WindowObjPtr objPtr;
- WindowObjHndl obj;
- GrafPtr oldPort;
-
- if (itFloats) listPosition = (WindowPtr) -1;
- obj = NULL;
- win = GetNewCWindow(windID, NULL, listPosition);
- check(win != NULL);
-
- if (win != NULL) {
- obj = (WindowObjHndl) NewHandle(sizeof(WindowObj));
-
- if (obj != NULL) {
- objPtr = *obj;
- objPtr->type = type;
- objPtr->hasIdleTask = hasIdleTask;
- objPtr->draw = draw;
- objPtr->keys = keys;
- objPtr->notify = notify;
- objPtr->activate = activate;
- objPtr->click = click;
- objPtr->window = win;
- objPtr->refCon = 0UL;
- }
- //
- // Put in those things that make our windows special..
- // And call the windows's create routine if there is one.
- //
- if (itFloats)
- ((WindowPeek) win)->windowKind = kApplicationFloaterKind;
- else
- ((WindowPeek) win)->windowKind = kAppWindowKind;
- SetWRefCon(win, (long) obj);
-
- oldPort = SetupPort(win);
- NotifyWindow(win, kCreateNotification);
-
- //
- // Call this after the refCon has been set, and after the
- // windowKind is set up so we can figure out what kind of window
- // this is from the PutInList call below.
- //
- // The create proc should set the window kind properly so this
- // all works..
- //
- #if defined(qFloatingWindowSupport)
- if (!IsFloatingWindow(win))
- PutNonFloatingWindowInList(win, (WindowPtr) -1);
-
- ShowThisWindow(win);
- SelectThisWindow(win);
- #endif
-
- SetPort(oldPort);
- }
- return obj;
- }
-
- //-----------------------------------------------------------------------
-
- void DisposeObjWindow(WindowObjHndl obj)
- {
- WindowPtr win = (*obj)->window;
- GrafPtr oldPort;
-
- check(win != NULL);
- //
- // Call the windows's destroy routine if there is one
- //
-
- oldPort = SetupPort(win);
- NotifyWindow(win, kDestroyNotification);
- HideThisWindow(win);
- SetPort(oldPort);
- DisposeWindow(win);
- }
-
- //-----------------------------------------------------------------------
-
- void InitApplication(void)
- {
- Handle theMenu;
-
- MaxApplZone();
- InitGraf(&qd.thePort);
- InitFonts();
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs(NULL);
- InitCursor();
- FlushEvents(0, everyEvent);
-
- gInBackground = gDone = false;
- theMenu = GetNewMBar(128);
-
- if (theMenu != NULL) {
- SetMenuBar(theMenu);
- AddResMenu(GetMHandle(128), 'DRVR');
- DrawMenuBar();
- }
- else {
- DebugStr("\pError loading the menus. Missing resource file?");
- gDone = true;
- }
- }
-
- //-----------------------------------------------------------------------
-
- void MainEventLoop(void)
- {
- EventRecord event;
- WindowPtr win;
- short clickArea;
- Rect screenRect;
- long result;
- char charCode;
-
- while (!gDone) {
- if (WaitNextEvent(everyEvent, &event, 60, NULL)) {
- switch (event.what) {
- case mouseDown:
- clickArea = FindWindow(event.where, &win);
-
- switch(clickArea) {
- case inDrag:
- screenRect = (**GetGrayRgn()).rgnBBox;
- #if defined(qFloatingWindowSupport)
- DragThisWindow(win, event.where, &screenRect); break;
- #else
- DragWindow(win, event.where, &screenRect);
- #endif
- case inContent:
- #if defined(qFloatingWindowSupport)
- if (NeedsSelect(win))
- SelectThisWindow(win);
- #else
- if (win != FrontWindow())
- SelectWindow(win);
- #endif
- else
- DoClick(win, &event, (long) clickArea);
- break;
-
- case inGoAway:
- if ((IsAppWindow(win)) && (TrackGoAway(win, event.where)))
- DoClose(win);
- break;
-
- case inMenuBar:
- DoAdjustMenus();
- result = MenuSelect(event.where);
- if ((result >> 16) != 0) MenuCommand(result);
- break;
-
- case inSysWindow:
- SystemClick(&event, win);
- break;
-
- case inGrow:
- DoClick(win, &event, (long) clickArea);
- break;
-
- default:
- break;
- }
- break;
-
- case keyDown:
- charCode = event.message & charCodeMask;
-
- if ((event.modifiers & cmdKey) != 0) {
- DoAdjustMenus();
- result = MenuKey(charCode);
- if ((result >> 16) != 0) MenuCommand(result);
- }
- else
- DoKeys(event.message, event.modifiers);
- break;
-
- case updateEvt:
- win = (WindowPtr) event.message;
- DoUpdate(win);
- break;
-
- case activateEvt:
- win = (WindowPtr) event.message;
- DoActivate(win, (event.modifiers & activeFlag) != 0);
- break;
-
- case osEvt:
- if (event.message & 0x01000000) {
- gInBackground = !gInBackground;
- #if defined(qFloatingWindowSupport)
- if (gInBackground)
- SuspendFloaters();
- else
- ResumeFloaters();
- #else
- win = FrontWindow();
- if (IsAppWindow(win))
- DoActivate(win, !gInBackground);
- #endif
- }
- }
- }
- else
- DoIdle();
- }
- }
-
- //-----------------------------------------------------------------------
-
- void MenuCommand(long whaHappened)
- {
- short menuID, menuItem;
- WindowPtr win = FrontNonFloatingWindow();
- extern void OpenFloatWindow(void);
-
- menuID = (whaHappened >> 16);
- menuItem = (whaHappened & 0xFFFF);
-
- switch (menuID) {
- case kAppleMenu:
- if (menuItem == kAboutItem) DoAboutBox();
- break;
-
- case kFileMenu:
- switch (menuItem) {
- case kNewItem:
- (void) NewObjWindow(128, 'Main', false, true, MainWindowDraw,
- MainWindowKeys, MainWindowClick, NULL,
- MainWindowNotify);
- break;
-
- case kOpenItem:
- OpenFloatWindow();
- break;
-
- case kCloseItem:
- if (IsAppWindow(win))
- DoClose(win);
- break;
- case kQuitItem:
- gDone = true;
- break;
- }
- break;
-
- case kEditMenu:
- DoEditMenu(win, menuItem);
- break;
- }
- HiliteMenu(0);
- }
-
- //-----------------------------------------------------------------------
-
- Boolean IsAppWindow(WindowPtr win)
- {
- Boolean oneOfOurs = false;
-
- if ((win != NULL)
- #if defined (qFloatingWindowSupport)
- && ((((WindowPeek) win)->windowKind == kAppWindowKind)
- || (IsFloatingWindow(win) == true)))
- #else
- && (((WindowPeek) win)->windowKind == kAppWindowKind))
- #endif
- oneOfOurs = true;
-
- return oneOfOurs;
- }
-
- //-----------------------------------------------------------------------
-
- GrafPtr SetupPort(GrafPtr port)
- {
- GrafPtr oldPort;
-
- check(port != NULL);
- GetPort(&oldPort);
- if (oldPort != port) SetPort(port);
- return oldPort;
- }
-
- //-----------------------------------------------------------------------
-
- void main(void)
- {
- InitApplication();
- PreEventLoop();
- MainEventLoop();
- PostEventLoop();
- }
-
- //-----------------------------------------------------------------------
-
- void pstrcat(Str255 frontStr, ConstStr255Param backStr)
- {
- short backLength = backStr[0];
-
- if (frontStr[0] + backLength > 255)
- backLength = 255 - frontStr[0];
-
- BlockMoveData(&backStr[1], &frontStr[frontStr[0] + 1], backLength);
- frontStr[0] += backLength;
- }
-
- //-----------------------------------------------------------------------
-
- void pstrcpy(void *src, void *dest)
- {
- BlockMoveData(src, dest,(*(Str255 *)src)[0] + 1);
- }
-
-
- #pragma segment AboutBox
- //-----------------------------------------------------------------------
- // •• ABOUTBOX WINDOW ••
- //-----------------------------------------------------------------------
-
- static WindowObjHndl pAboutBox = NULL;
-
-
- //-----------------------------------------------------------------------
-
- void DoAboutBox(void)
- {
- if (pAboutBox == NULL)
- pAboutBox = NewObjWindow(129, 'Abot', false, false, AboutBoxDraw, AboutBoxKeys,
- AboutBoxClick, AboutBoxActivate, AboutBoxNotify);
- else {
- ShowThisWindow((*pAboutBox)->window);
- SelectThisWindow((*pAboutBox)->window);
- }
- }
-
- //-----------------------------------------------------------------------
-
- void DisableAllMenuItems(MenuHandle menu)
- {
- short index, count = CountMItems(menu);
-
- for (index = 1; index <= count; index ++)
- DisableItem(menu, index);
- }
-
- //-----------------------------------------------------------------------
-
- void EnableAllMenuItems(MenuHandle menu)
- {
- short index, count = CountMItems(menu);
-
- for (index = 1; index <= count; index ++)
- EnableItem(menu, index);
- }
-
- //-----------------------------------------------------------------------
-
- void AboutBoxNotify(WindowObjHndl obj, long message)
- {
- ControlHandle cntl;
- WindowPtr win = (*obj)->window;
- Rect rct;
-
- switch (message) {
- case kCloseNotification:
- break;
-
- case kCreateNotification:
- TextFont(systemFont);
- TextSize(12);
- rct = win->portRect;
- SetRect(&rct, rct.right - 100, rct.bottom - 35,
- rct.right - 20, rct.bottom - 15);
- cntl = NewControl(win, &rct, "\pOK", true, 0, 0, 1, pushButProc, 0);
- (*obj)->refCon = (unsigned long) cntl;
- break;
-
- case kDestroyNotification:
- cntl = (ControlHandle) (*obj)->refCon;
-
- if (cntl != NULL) DisposeControl(cntl);
- break;
-
- case kAdjustMenusNotification: {
- MenuHandle fileMenu = GetMenuHandle(kFileMenu);
- MenuHandle editMenu = GetMenuHandle(kEditMenu);
-
- if (fileMenu != NULL) {
- EnableAllMenuItems(fileMenu);
-
- EnableItem(fileMenu, kCloseItem);
- DisableItem(fileMenu, kNewItem);
- DisableItem(fileMenu, kOpenItem);
- }
-
- if (editMenu != NULL)
- DisableAllMenuItems(editMenu);
- }
- break;
- }
- }
-
- //-----------------------------------------------------------------------
-
- void AboutBoxDraw(WindowObjHndl obj, short depth)
- {
- #pragma unused (depth)
- Rect rct, box;
- WindowPtr win = (*obj)->window,
- #if defined(qFloatingWindowSupport)
- front = FrontNonFloatingWindow();
- #else
- front = FrontWindow();
- #endif
- Handle text;
- Size textSize;
- ControlHandle cntl = (ControlHandle) (*obj)->refCon;
- short hiliteVal;
-
- check(cntl != NULL);
- PenNormal();
-
- if ((gInBackground == true) || (front != win)) {
- RGBColor rgb = {0x7fff, 0x7fff, 0x7fff};
- RGBForeColor(&rgb);
- hiliteVal = 255;
- }
- else {
- ForeColor(blackColor);
- hiliteVal = 0;
- }
-
- HiliteControl(cntl, hiliteVal);
- DrawControls(win);
- rct = (*(ControlHandle) (*obj)->refCon)->contrlRect;
- PenSize(3, 3);
- InsetRect(&rct, -4, -4);
- FrameRoundRect(&rct, 16, 16);
- text = Get1Resource('TEXT', 128);
-
- if (text != NULL) {
- textSize = GetHandleSize(text);
- box = win->portRect;
- InsetRect(&box, 15, 10);
- box.bottom -= 45;
- HLock(text);
- TextBox(*text, textSize, &box, teForceLeft);
- HUnlock(text);
- }
- }
-
- //-----------------------------------------------------------------------
-
- void AboutBoxClick(WindowObjHndl obj, EventRecord *event, long message)
- {
- ControlHandle button;
- short part;
- WindowPtr win = (*obj)->window;
- Point localPt;
-
- if (message == inContent) {
- localPt = event->where;
- GlobalToLocal(&localPt);
-
- part = FindControl(localPt, win, &button);
-
- if ((part == inButton) && (button != NULL))
- if (TrackControl(button, event->where, NULL) != 0)
- HideThisWindow(win);
- }
- }
-
- //-----------------------------------------------------------------------
- // If the user hit enter, fake an 'OK' hilite and hide the window.
- //-----------------------------------------------------------------------
- void AboutBoxKeys(WindowObjHndl obj, long message, short mods)
- {
- #pragma unused (mods)
- unsigned char code;
- ControlHandle button;
- long dontCare;
-
- code = (message & keyCodeMask) >> 8;
-
- if ((code == 0x24) || (code == 0x4C)) {
- button = (ControlHandle) (*obj)->refCon;
-
- if (button != NULL) {
- HiliteControl(button, true);
- Delay(8, &dontCare);
- HiliteControl(button, false);
- }
- HideThisWindow((*obj)->window);
- }
- }
-
- //-----------------------------------------------------------------------
- // All the drawing is done in the update proc, so just invalidate the
- // whole window to force a complete redraw.
- //-----------------------------------------------------------------------
- void AboutBoxActivate(WindowObjHndl obj, Boolean activate)
- {
- #pragma unused (activate)
-
- InvalRect(&(*obj)->window->portRect);
- }